//
//  IntegralOtherShopXQViewController.m
//  lmq
//
//  Created by 马田成 on 2017/4/13.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "IntegralOtherShopXQViewController.h"
#import "IntegralOtherShopXQTableViewCell.h"
#import "Masonry.h"
#import "WXApi.h"
#import "configddd.h"
#import "ActivitiesPublishViewController.h"
#import "AllParticipateRecordsViewController.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "IntegralMallDetailsModel.h"

#import "IntegralMallParticipateView.h"
#import "IntegralMallClearingVC.h"
#import <MJRefresh/MJRefresh.h>
#import "addressViewController.h"

#import "ToolAboutTime.h"
#import "IntegralMallDetailsViewController.h"


@interface IntegralOtherShopXQViewController ()<UITableViewDelegate, UITableViewDataSource,SDCycleScrollViewDelegate>

{
    UIView *grayView;
    
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *dataArr;
@property (nonatomic, strong) UIButton *rightButton;
@property(nonatomic,strong)UIView *shadow;
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong)IntegralMallParticipateView *nowView;

@property (nonatomic, strong) NSMutableArray *smallPicArr;
@property (nonatomic, strong) NSMutableArray *bigPicArr;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, assign) NSInteger data;
@property (nonatomic, strong) NSString *DataOfNew;//是否有新的一期

@end

@implementation IntegralOtherShopXQViewController
//- (void)viewWillAppear:(BOOL)animated{
//    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
//    [self.tableView.mj_footer endRefreshing];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creationDate];
    
    [self creationView];
    [self regeditCell];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.navigationItem.title = @"商品详情";
    //    self.tableView = [[UITableView alloc] init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    self.shadow = [[UIView alloc]init];
    
    
    
    self.rightButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(35), screenScale(35))];
    self.rightButton.selected = NO;
    [self.rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateSelected];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:self.rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"IntegralOtherShopXQTableViewCell" bundle:nil] forCellReuseIdentifier:@"IntegralOtherShopXQTableViewCell"];
    
}

- (void)setShadow:(UIView *)shadow{
    
    _shadow = shadow;
    _shadow.frame = CGRectMake(0, DEF_SCREEN_HEIGHT, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    _shadow.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    _shadow.hidden = YES;
    [[[UIApplication sharedApplication]keyWindow]addSubview:_shadow];
    
    //分享背景
    
    UIView *shareView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1136 - 278), DEF_SCREEN_WIDTH, screenScale(280))];
    shareView.backgroundColor = [UIColor whiteColor];
    [_shadow addSubview:shareView];
    
    //微信btn
    
    UIButton *wechatBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenScale(180), screenScale(62), screenScale(84), screenScale(100))];
    [wechatBtn addTarget:self action:@selector(wechatBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:wechatBtn];
    
    UIImageView *wechatImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), 0, screenScale(68), screenScale(68))];
    wechatImg.image = [UIImage imageNamed:@"wechat"];
    [wechatBtn addSubview:wechatImg];
    
    UILabel *wechatLable = [[UILabel alloc]initWithFrame:CGRectMake(0, screenScale(75), screenScale(84), screenScale(30))];
    wechatLable.text = @"微信";
    wechatLable.textAlignment = NSTextAlignmentCenter;
    wechatLable.font = get4sFont(13);
    [wechatBtn addSubview:wechatLable];
    
    //朋友圈btn
    
    UIButton *friendsBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenScale(380), screenScale(62), screenScale(84), screenScale(100))];
    [friendsBtn addTarget:self action:@selector(friendsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:friendsBtn];
    
    UIImageView *friendsImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), 0, screenScale(68), screenScale(68))];
    friendsImg.image = [UIImage imageNamed:@"friends"];
    [friendsBtn addSubview:friendsImg];
    
    UILabel *friendLable = [[UILabel alloc]initWithFrame:CGRectMake(0, screenScale(75), screenScale(84), screenScale(30))];
    friendLable.text = @"朋友圈";
    friendLable.textAlignment = NSTextAlignmentCenter;
    friendLable.font = get4sFont(13);
    [friendsBtn addSubview:friendLable];
    
    //取消
    
    UIView *thread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(193), scrWidth() - screenScale(40), screenScale(1))];
    thread.backgroundColor = getColor(@"dcdcdc");
    [shareView addSubview:thread];
    
    UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, screenScale(194), scrWidth(), screenScale(85))];
    cancelBtn.titleLabel.font = get4sFont(15);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:cancelBtn];
}

- (void)wechatBtnClick{
    
    NSLog(@"微信分享");
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"乐麦圈";
    message.description = @"";
    [message setThumbImage:[UIImage imageNamed:@""]];
    
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = @"http://lmq.quyum.cn/home/register/regweb/invitation/a4bi31j1";
    message.mediaObject = webpageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;
    
    [WXApi sendReq:req];
}

- (void)friendsBtnClick{
    
    NSLog(@"朋友圈分享");
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"乐麦圈";
    message.description = @"";
    [message setThumbImage:[UIImage imageNamed:@""]];
    
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = @"http://lmq.quyum.cn/home/register/regweb/invitation/a4bi31j1";
    message.mediaObject = webpageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];
}

- (void)cancelBtnClick{
    
    NSLog(@"取消");
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    _shadow.frame = CGRectMake(0, DEF_SCREEN_HEIGHT, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    [UIView commitAnimations];
    _shadow.hidden = YES;
}

- (void)creationDate{
    
    _dataArr = [[NSMutableDictionary alloc]init];
    _arr = [[NSMutableArray alloc]init];
    _smallPicArr = [NSMutableArray array];
    _bigPicArr = [NSMutableArray array];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:_addTime forKey:@"stage"];
    [productParams setValue:_shopid forKey:@"shop_id"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:memberId forKey:@"member_id"];
    
    [[MNetworkHandler sharedInstance]conURL:Home_OneShowPastShop
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"details"];
                                       ;
                                       _dataArr = [returnData objectForKey:@"details"];
                                       _smallPicArr = [dict1 objectForKey:@"carousel"];
                                       
                                       
                                       
                                       
                                       [_arr addObject:_dataArr];
                                       
                                       
                                       
                                       [self.tableView reloadData];
                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
 //中间cell数据
    [[MNetworkHandler sharedInstance]conURL:Home_OneShowAttend
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==-3) {
                                    
                                       
                                       _data = [[returnData objectForKey:@"data"] integerValue];
                                       
                                       [self.tableView reloadData];
                                   }
                                   else {
                                       
                                       _data = 0;
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
    
    
    
    
    
    
    
}


#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//自定义section的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *lunBoView = [[UIView alloc] init];
    
    lunBoView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 200);
    //    NSArray *arrMu = @[@"liyi",@"liyi",@"liyi"];
    self.cycleScrollView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, -35, DEF_SCREEN_WIDTH, 200) imageNamesGroup:self.smallPicArr];
    self.cycleScrollView.backgroundColor=[UIColor greenColor];
    self.cycleScrollView.pageControlAliment=SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageDotColor = [UIColor grayColor]; // 自定义分页控件小圆标颜色
    
    NSMutableArray *myarr=[[NSMutableArray alloc]init];
    for (int i=0; i<self.smallPicArr.count; i++) {
        
        NSString *str=[self.smallPicArr[i] objectForKey:@"pic"];
        NSString *urlN=manage(AP_HOST, str);
        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        [myarr addObject:strUrl];
    }
    [self.cycleScrollView setLocalizationImageNamesGroup:myarr];
    [self.cycleScrollView reloadInputViews];
    self.cycleScrollView.currentPageDotImage=[UIImage imageNamed:@"hei"];
    self.cycleScrollView.pageDotImage=[UIImage imageNamed:@"hui"];
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@""];
    [lunBoView addSubview:self.cycleScrollView];
    
    return lunBoView;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", index);
    
}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    
    
    return 200;
    
    
}
//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 200; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
    
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;

    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 310;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    IntegralOtherShopXQTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"IntegralOtherShopXQTableViewCell" forIndexPath:indexPath];
    

    cell.peopleNameLabel.text = [_dataArr objectForKey:@"user_name"];
    cell.qiHaoLabel.text = [_dataArr objectForKey:@"stage"];
    cell.canYuNumber.text = [_dataArr objectForKey:@"num"];
    cell.jieTimeLabel.text = [ToolAboutTime getTimeStrByTimeSp:[_dataArr objectForKey:@"time"]];
    NSString *strLucky = @"幸运号码:";
    cell.luckyLabel.text = [strLucky stringByAppendingString:[_dataArr objectForKey:@"code"]];
    
    
    
//    NSString *strPic = @"http://112.74.36.208/";
//    
//    NSString *str = [strPic stringByAppendingString:[_dataArr objectForKey:@"header_img"]];
    
    NSString *urlWare=[_dataArr objectForKey:@"shoppic"];
    NSString *urlN=manage(AP_HOST, urlWare);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    
    if (_data == 0) {
        cell.ninCanYLabel.text = @"您没有参加本次夺宝";
    }else {
        
        NSString *dataO = @"您参加了本期";
        NSString *numOfData = [NSString stringWithFormat:@"%ld", (long)_data];
        
        cell.ninCanYLabel.text = [[dataO stringByAppendingString:numOfData] stringByAppendingString:@"次夺宝"];
        
    }
    
    [cell.touXImage sd_setImageWithURL:[NSURL URLWithString:strUrl]];
    
    [cell.buttonOfCanY addTarget:self action:@selector(allParticiateRClick) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.buttonGoNow addTarget:self action:@selector(activitiesPublishVC) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
    
    
}
#pragma mark - 点击右导航
-(void)rightNavItemClick{
    
    NSLog(@"分享");
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    _shadow.hidden = NO;
    _shadow.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    [UIView commitAnimations];
}
//立即前往
- (void)activitiesPublishVC{
    

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:_addTime forKey:@"stage"];
    [productParams setValue:_shopid forKey:@"shop_id"];

    [[MNetworkHandler sharedInstance]conURL:Home_OneShowNewStage
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"data"]intValue]==1) {
                                       
                                       IntegralMallDetailsViewController *vc = [[IntegralMallDetailsViewController alloc] init];
                                       vc.hidesBottomBarWhenPushed = YES;
                                       
                                       vc.shopid = _shopid;
                                       vc.addTime = _addTime;
                                       
                                       
                                       [self.navigationController pushViewController:vc animated:YES];
                                       
                                       
                                       [self.tableView reloadData];
                                   }
                                   else {
                                       
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

    
    
    
    
}
//_dataArr所有参与记录
- (void)allParticiateRClick{
    
    AllParticipateRecordsViewController *vc = [[AllParticipateRecordsViewController alloc] init];
    
    vc.shop_id = _shopid;
    vc.stage = _addTime;
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
//点击隐藏view
//-(void)SingleTap:(UITapGestureRecognizer*)recognizer  {
//    
//    
//    self.nowView.hidden = YES;
//    grayView.hidden = YES;
//    
//    
//}
//- (void)canceClick{
//    
//    self.nowView.hidden = YES;
//    grayView.hidden = YES;
//    
//}
//- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    
//    footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
//    footerView.backgroundColor = getColor(@"f5f5f5");
//    
//    //刷新之后
//    footerView2 = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(10), scrWidth(), footViewHeight)];
//    footerView2.hidden = !hidden;
//    [footerView addSubview:footerView2];
//    
//    for (int i = 0; i < _bigPicArr.count; i ++) {
//        
//        int height = 0;
//        
//        for (int j = 0 ; j < i ; j ++) {
//            
//            height += [picArr[j] intValue];
//        }
//        
//        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,height, scrWidth(), [picArr[i] intValue])];
//        [footerView2 addSubview:img];
//        
//        NSString *urlWare = [self.bigPicArr[i] objectForKey:@"pic"];
//        NSString *urlN = manage(AP_HOST, urlWare);
//        
//        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
//        NSURL *urlW = [NSURL URLWithString:strUrl];
//        [img sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
//    }
//    
//    //上拉刷新文字提示
//    footerView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
//    footerView1.backgroundColor = getColor(@"f5f5f5");
//    footerView1.hidden = hidden;
//    [footerView addSubview:footerView1];
//    
//    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(1))];
//    threadView.backgroundColor = getColor(@"dfdfdf");
//    [footerView1 addSubview:threadView];
//    
//    UILabel *footLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
//    footLable.text = @"继续拖动，查看图文详情";
//    footLable.font = get4sFont(11);
//    footLable.textAlignment = NSTextAlignmentCenter;
//    [footerView1 addSubview:footLable];
//    
//    return footerView;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    
//    return footViewHeight + screenScale(10);
//}
//- (void)loadMoreTopic{
//    
//    [self.tableView.mj_footer endRefreshing];
//    
//    picArr = [[NSMutableArray alloc]init];
//    footViewHeight = 0;
//    
//    for (int i = 0 ; i < _bigPicArr.count; i ++) {
//        
//        NSString *urlGood = _bigPicArr[i][@"pic"];
//        NSString *urlN = manage(AP_HOST, urlGood);
//        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
//        
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl]];
//        UIImage *image = [UIImage imageWithData:data];
//        size = image.size;
//        
//        footViewHeight += size.height/(size.width/scrWidth());
//        [picArr insertObject:[NSString stringWithFormat:@"%f",size.height/(size.width/scrWidth())] atIndex:i];
//    }
//    
//    if (_bigPicArr.count > 0) {
//        
//        hidden = YES;
//    }
//    NSLog(@"%@",picArr);
//    [self.tableView reloadData];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
